﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CYTools.Models.Model;
using CYTools.Models.Service;
using CYTools.Util;
using System.Collections;
using CookComputing.XmlRpc;

namespace CYTools
{
    public partial class CYTools : Form
    {
        public bool flg = true;
        public string odoo_dbname;
        public int odoo_uid;
        public string odoo_pwd;
        public string odoo_objname;
        public CYTools()
        {
            odoo_dbname = System.Configuration.ConfigurationManager.AppSettings["odoo_dbname"];
            odoo_uid = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["odoo_uid"]);
            odoo_pwd = System.Configuration.ConfigurationManager.AppSettings["odoo_pwd"];
            odoo_objname = System.Configuration.ConfigurationManager.AppSettings["odoo_objname"];
            InitializeComponent();
        }
        /// <summary>
        /// 同步客户信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void kehubutton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultTextBox.Text = "";
                this.kehubutton.Enabled = false;
                //判断是否有正在进行的同步
                if (flg)
                {
                    flg = false;
                    KehuInfoService kService = new KehuInfoService();
                    List<KehuInfo> lists = kService.getKehuInfoList();
                    this.progressBar.Value = 0;
                    this.progressBar.Maximum = lists.Count();
                    foreach (KehuInfo list in lists)
                    {
                        Hashtable partner = new Hashtable();
                        partner.Add("cy_partner_id", list.Kehu_id);
                        partner.Add("partner_code", list.Bianma);
                        partner.Add("name", list.KeHuMingcheng);
                        partner.Add("child_name", list.LianXiRen);
                        partner.Add("phone", list.DianHua);
                        partner.Add("mobile", list.ShouJi);
                        partner.Add("zip", list.YouZhengBianMa);
                        partner.Add("street", list.DiZhi);
                        IProxy.IMethod method = XmlRpcProxyGen.Create<IProxy.IMethod>();
                        object res = method.execute(odoo_dbname, odoo_uid, odoo_pwd, odoo_objname, "update_partner", Newtonsoft.Json.JsonConvert.SerializeObject(partner));
                        ResData resData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResData>(res.ToString());
                        this.resultTextBox.Text = resData.successOrNot + "|" + resData.message + "|" + resData.data;
                        this.progressBar.Value = this.progressBar.Value + 1;
                    }
                }
                else
                {
                    MessageBox.Show("其他同步正在进行中，请稍候", "error");
                }
                flg = true;
                this.kehubutton.Enabled = true;
            }
            catch (Exception ex)
            {
                flg = true;
                this.kehubutton.Enabled = true;
                LogManager.WriteLog(LogFile.ERROR,ex.Message);
                MessageBox.Show("error","error");
            }
        }
        /// <summary>
        /// 同步供应商信息(厂家)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gysbutton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultTextBox.Text = "";
                this.gysbutton.Enabled = false;
                //判断是否有正在进行的同步
                if (flg)
                {
                    flg = false;
                    CjInfoService cService = new CjInfoService();
                    List<CjInfo> lists = cService.getCjInfoList();
                    this.progressBar.Value = 0;
                    this.progressBar.Maximum = lists.Count();
                    foreach (CjInfo list in lists)
                    {
                        Hashtable partner = new Hashtable();
                        partner.Add("cy_supplier_id", list.Cj_id);
                        partner.Add("name", list.Quancheng);
                        partner.Add("cy_partner_type", list.Cjlb);
                        partner.Add("partner_code", list.Bianma);
                        IProxy.IMethod method = XmlRpcProxyGen.Create<IProxy.IMethod>();
                        object res = method.execute(odoo_dbname, odoo_uid, 
                            odoo_pwd, odoo_objname, "update_supplier", Newtonsoft.Json.JsonConvert.SerializeObject(partner));
                        ResData resData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResData>(res.ToString());
                        this.resultTextBox.Text = resData.successOrNot + "|" + resData.message + "|" + resData.data;
                        this.progressBar.Value = this.progressBar.Value + 1;
                    }
                }
                else
                {
                    MessageBox.Show("其他同步正在进行中，请稍候", "error");
                }
                flg = true;
                this.gysbutton.Enabled = true;
            }
            catch (Exception ex)
            {
                flg = true;
                this.gysbutton.Enabled = true;
                LogManager.WriteLog(LogFile.ERROR, ex.Message);
                MessageBox.Show("error", "error");
            }
        }
        /// <summary>
        /// 同步厂家的型材系列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xlbutton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultTextBox.Text = "";
                this.xlbutton.Enabled = false;
                //判断是否有正在进行的同步
                if (flg)
                {
                    flg = false;
                    YclInfoService yService = new YclInfoService();
                    List<XilieInfo> lists = yService.getXlInfoList();
                    this.progressBar.Value = 0;
                    this.progressBar.Maximum = lists.Count();
                    foreach (XilieInfo list in lists)
                    {
                        Hashtable partner = new Hashtable();
                        partner.Add("cymc_series_id", list.XL_id);
                        partner.Add("name", list.Mingcheng);
                        partner.Add("cymc_partner_id", list.Cj_id);
                        IProxy.IMethod method = XmlRpcProxyGen.Create<IProxy.IMethod>();
                        object res = method.execute(odoo_dbname, odoo_uid,
                            odoo_pwd, odoo_objname, "update_series", Newtonsoft.Json.JsonConvert.SerializeObject(partner));
                        ResData resData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResData>(res.ToString());
                        this.resultTextBox.Text = resData.successOrNot + "|" + resData.message + "|" + resData.data;
                        this.progressBar.Value = this.progressBar.Value + 1;
                    }
                }
                else
                {
                    MessageBox.Show("其他同步正在进行中，请稍候", "error");
                }
                flg = true;
                this.xlbutton.Enabled = true;
            }
            catch (Exception ex)
            {
                flg = true;
                this.xlbutton.Enabled = true;
                LogManager.WriteLog(LogFile.ERROR, ex.Message);
                MessageBox.Show("error", "error");
            }
        }
        /// <summary>
        /// 型材数据同步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xcbutton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultTextBox.Text = "";
                this.xcbutton.Enabled = false;
                //判断是否有正在进行的同步
                if (flg)
                {
                    flg = false;
                    YclInfoService yService = new YclInfoService();
                    List<XingCaiInfo> lists = yService.getXcInfoList();
                    this.progressBar.Value = 0;
                    this.progressBar.Maximum = lists.Count();
                    foreach (XingCaiInfo list in lists)
                    {
                        Hashtable partner = new Hashtable();
                        partner.Add("cymc_extrusions_id", list.Xc_id);
                        partner.Add("cymc_series_id", list.Xl_id);
                        partner.Add("name", list.MingchengXC);
                        partner.Add("default_code", list.Daima);
                        partner.Add("cymc_long", list.ChangDu);
                        partner.Add("list_price", list.JiaGe);
                        partner.Add("cymc_partner_id", list.Cj_ID);
                        partner.Add("cymc_color", list.YS);
                        IProxy.IMethod method = XmlRpcProxyGen.Create<IProxy.IMethod>();
                        object res = method.execute(odoo_dbname, odoo_uid,
                            odoo_pwd, odoo_objname, "update_extrusions", Newtonsoft.Json.JsonConvert.SerializeObject(partner));
                        ResData resData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResData>(res.ToString());
                        this.resultTextBox.Text = resData.successOrNot + "|" + resData.message + "|" + resData.data;
                        this.progressBar.Value = this.progressBar.Value + 1;
                    }
                }
                else
                {
                    MessageBox.Show("其他同步正在进行中，请稍候", "error");
                }
                flg = true;
                this.xcbutton.Enabled = true;
            }
            catch (Exception ex)
            {
                flg = true;
                this.xcbutton.Enabled = true;
                LogManager.WriteLog(LogFile.ERROR, ex.Message);
                MessageBox.Show("error", "error");
            }
        }
        /// <summary>
        /// 配件数据同步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pjbutton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultTextBox.Text = "";
                this.pjbutton.Enabled = false;
                //判断是否有正在进行的同步
                if (flg)
                {
                    flg = false;
                    YclInfoService yService = new YclInfoService();
                    List<PeiJianInfo> lists = yService.getPjInfoList();
                    this.progressBar.Value = 0;
                    this.progressBar.Maximum = lists.Count();
                    foreach (PeiJianInfo list in lists)
                    {
                        Hashtable partner = new Hashtable();
                        partner.Add("cymc_accessories_id", list.Pj_id);
                        partner.Add("name", list.Mingcheng);
                        partner.Add("cymc_partner_id", list.Cj_id);
                        partner.Add("default_code", list.Daima);
                        partner.Add("list_price", list.Danjia);
                        IProxy.IMethod method = XmlRpcProxyGen.Create<IProxy.IMethod>();
                        object res = method.execute(odoo_dbname, odoo_uid,
                            odoo_pwd, odoo_objname, "update_accessories", Newtonsoft.Json.JsonConvert.SerializeObject(partner));
                        ResData resData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResData>(res.ToString());
                        this.resultTextBox.Text = resData.successOrNot + "|" + resData.message + "|" + resData.data;
                        this.progressBar.Value = this.progressBar.Value + 1;
                    }
                }
                else
                {
                    MessageBox.Show("其他同步正在进行中，请稍候", "error");
                }
                flg = true;
                this.pjbutton.Enabled = true;
            }
            catch (Exception ex)
            {
                flg = true;
                this.pjbutton.Enabled = true;
                LogManager.WriteLog(LogFile.ERROR, ex.Message);
                MessageBox.Show("error", "error");
            }
        }
        /// <summary>
        /// 玻璃数据同步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void blbutton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultTextBox.Text = "";
                this.blbutton.Enabled = false;
                //判断是否有正在进行的同步
                if (flg)
                {
                    flg = false;
                    YclInfoService yService = new YclInfoService();
                    List<BoLiInfo> lists = yService.getBlInfoList();
                    this.progressBar.Value = 0;
                    this.progressBar.Maximum = lists.Count();
                    foreach (BoLiInfo list in lists)
                    {
                        Hashtable partner = new Hashtable();
                        partner.Add("cymc_glass_id", list.Bl_id);
                        partner.Add("name", list.HouDu+" "+list.YanSe);
                        partner.Add("default_code", list.Daima);
                        partner.Add("cymc_partner_id", list.Cj_id);
                        partner.Add("cymc_long", list.Changdu);
                        partner.Add("cymc_wide", list.Kuandu);
                        partner.Add("cymc_land", list.HouDu);
                        partner.Add("cymc_color", list.YanSe);
                        partner.Add("list_price", list.DanJia);
                        IProxy.IMethod method = XmlRpcProxyGen.Create<IProxy.IMethod>();
                        object res = method.execute(odoo_dbname, odoo_uid,
                            odoo_pwd, odoo_objname, "update_glass", Newtonsoft.Json.JsonConvert.SerializeObject(partner));
                        ResData resData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResData>(res.ToString());
                        this.resultTextBox.Text = resData.successOrNot + "|" + resData.message + "|" + resData.data;
                        this.progressBar.Value = this.progressBar.Value + 1;
                    }
                }
                else
                {
                    MessageBox.Show("其他同步正在进行中，请稍候", "error");
                }
                flg = true;
                this.blbutton.Enabled = true;
            }
            catch (Exception ex)
            {
                flg = true;
                this.blbutton.Enabled = true;
                LogManager.WriteLog(LogFile.ERROR, ex.Message);
                MessageBox.Show("error", "error");
            }
        }
        /// <summary>
        /// 工程信息同步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void projectbutton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultTextBox.Text = "";
                this.projectbutton.Enabled = false;
                //判断是否有正在进行的同步
                if (flg)
                {
                    flg = false;
                    ProjectInfoService yService = new ProjectInfoService();
                    List<ProjectInfo> lists = yService.getPjInfoList();
                    this.progressBar.Value = 0;
                    this.progressBar.Maximum = lists.Count();
                    foreach (ProjectInfo list in lists)
                    {
                        //工程信息
                        Hashtable project = new Hashtable();
                        project.Add("cy_sale_id", list.Gcxx_id);
                        project.Add("cy_sale_name", list.Mingcheng);
                        project.Add("cy_partner_id", list.Kehu_id);
                        //工程门窗列表
                        List<ProjectWindow> windowlists = yService.getPjwInfoList(list.Gcxx_id);
                        object[] windows = new object[windowlists.Count()];
                        int i = 0;
                        foreach (ProjectWindow window in windowlists)
                        {
                            Hashtable w = new Hashtable();
                            w.Add("cymc_sale_product_id", window.Gcmc_id);
                            w.Add("name", "窗型"+(i+1).ToString());
                            w.Add("default_code", window.Daima);
                            w.Add("product_uom_qty", window.Shuliang);
                            w.Add("cymc_long", window.Chang);
                            w.Add("cymc_wide", window.Kuan);
                            
                            //工程门窗明细
                            List<ProjectWindowDetail> windowdetaillists = yService.getPjwDInfoList(window.Gcmc_id);
                            object[] windowdetails = new object[windowdetaillists.Count()];
                            int j = 0;
                            foreach (ProjectWindowDetail windowdetail in windowdetaillists)
                            {
                                //单窗型,直接传递原材料BOM
                                if (windowdetaillists.Count() == 1)
                                {
                                    //门窗明细的型材下料
                                    YclInfoService yclService = new YclInfoService();
                                    List<XcXlInfo> xcxllists = yService.getXcXlInfoList(windowdetail.Mcmx_id);
                                    object[] xcxls = new object[xcxllists.Count()];
                                    int k=0;
                                    foreach(XcXlInfo xcxl in xcxllists)
                                    {
                                        Hashtable xc = new Hashtable();
                                        xc.Add("cymc_bom_extrusions_id", xcxl.Xcxl_id);
                                        xc.Add("cymc_extrusions_product", xcxl.Xc_id);
                                        //取得型材名称
                                        XingCaiInfo XcInfo = yclService.getXcDetailInfo(xcxl.Xc_id);
                                        xc.Add("name", XcInfo.MingchengXC+" "+xcxl.Dir);
                                        //计算出单价
                                        xc.Add("extrusions_price", String.Format("{0:F}", (double.Parse(XcInfo.JiaGe) / double.Parse(XcInfo.ChangDu)) * double.Parse(xcxl.Changdu)));
                                        xc.Add("cymc_long", xcxl.Changdu);
                                        xc.Add("product_qty", xcxl.ShuLiang);
                                        xcxls[k] = xc;
                                        k++;
                                    }
                                    w.Add("bom_extrusions_lines", xcxls);
                                    //门窗明细的配件下料
                                    List<PjXlInfo> pjxllists = yService.getPjXlInfoList(windowdetail.Mcmx_id);
                                    object[] pjxls = new object[pjxllists.Count()];
                                    int m = 0;
                                    foreach (PjXlInfo pjxl in pjxllists)
                                    {
                                        Hashtable pj = new Hashtable();
                                        pj.Add("cymc_bom_accessories_id", pjxl.Pjxl_id);
                                        pj.Add("cymc_accessories_id", pjxl.Pj_id);
                                        //取得配件单价
                                        PeiJianInfo PjInfo = yclService.getPjDetailInfo(pjxl.Pj_id);
                                        pj.Add("accessories_price", PjInfo.Danjia);
                                        pj.Add("product_qty", pjxl.Shuliang);
                                        pjxls[m] = pj;
                                        m++;
                                    }
                                    w.Add("bom_accessories_lines", pjxls);
                                    //门窗明细的玻璃下料
                                    List<BlXlInfo> blxllists = yService.getBlXlInfoList(windowdetail.Mcmx_id);
                                    object[] blxls = new object[blxllists.Count()];
                                    int n = 0;
                                    foreach (BlXlInfo blxl in blxllists)
                                    {
                                        Hashtable bl = new Hashtable();
                                        bl.Add("cymc_bom_glass_id", blxl.Blxl_id);
                                        bl.Add("cymc_long", blxl.Changdu);
                                        bl.Add("cymc_wide", blxl.Kuandu);
                                        string[] value =  blxl.WinBLZL.Split(' ');
                                        bl.Add("cymc_land", value[0]);
                                        bl.Add("cymc_color", value[1]);
                                        bl.Add("name", blxl.WinBLZL);
                                        bl.Add("glass_price", blxl.BLFee);
                                        bl.Add("product_qty", blxl.Shuliang);
                                        blxls[n] = bl;
                                        n++;
                                    }
                                    w.Add("bom_glass_lines", blxls);
                                }
                                //组合窗型,先传递组合明细的BOM，然后传递明细的原材料BOM
                                else
                                {
                                    Hashtable wd = new Hashtable();
                                    wd.Add("cymc_bom_product_id", windowdetail.Mcmx_id);
                                    if (windowdetail.Tk_id == "1.0000")
                                    {
                                        wd.Add("name", "拼接料");
                                    }
                                    else
                                    {
                                        wd.Add("name", "窗型"+(j+1).ToString());
                                    }
                                    
                                    wd.Add("cymc_series_id", windowdetail.Xl_id);
                                    //门窗明细的型材下料
                                    YclInfoService yclService = new YclInfoService();
                                    List<XcXlInfo> xcxllists = yService.getXcXlInfoList(windowdetail.Mcmx_id);
                                    object[] xcxls = new object[xcxllists.Count()];
                                    int k = 0;
                                    foreach (XcXlInfo xcxl in xcxllists)
                                    {
                                        Hashtable xc = new Hashtable();
                                        xc.Add("cymc_bom_extrusions_id", xcxl.Xcxl_id);
                                        xc.Add("cymc_extrusions_product", xcxl.Xc_id);
                                        //取得型材名称
                                        XingCaiInfo XcInfo = yclService.getXcDetailInfo(xcxl.Xc_id);
                                        xc.Add("name", XcInfo.MingchengXC + " " + xcxl.Dir);
                                        //计算出单价
                                        xc.Add("extrusions_price", String.Format("{0:F}", (double.Parse(XcInfo.JiaGe) / double.Parse(XcInfo.ChangDu)) * double.Parse(xcxl.Changdu)));
                                        xc.Add("cymc_long", xcxl.Changdu);
                                        xc.Add("product_qty", xcxl.ShuLiang);
                                        xcxls[k] = xc;
                                        k++;
                                    }
                                    wd.Add("bom_extrusions_lines", xcxls);
                                    //门窗明细的配件下料
                                    List<PjXlInfo> pjxllists = yService.getPjXlInfoList(windowdetail.Mcmx_id);
                                    object[] pjxls = new object[pjxllists.Count()];
                                    int m = 0;
                                    foreach (PjXlInfo pjxl in pjxllists)
                                    {
                                        Hashtable pj = new Hashtable();
                                        pj.Add("cymc_bom_accessories_id", pjxl.Pjxl_id);
                                        pj.Add("cymc_accessories_id", pjxl.Pj_id);
                                        //取得配件单价
                                        PeiJianInfo PjInfo = yclService.getPjDetailInfo(pjxl.Pj_id);
                                        pj.Add("accessories_price", PjInfo.Danjia);
                                        pj.Add("product_qty", pjxl.Shuliang);
                                        pjxls[m] = pj;
                                        m++;
                                    }
                                    wd.Add("bom_accessories_lines", pjxls);
                                    //门窗明细的玻璃下料
                                    List<BlXlInfo> blxllists = yService.getBlXlInfoList(windowdetail.Mcmx_id);
                                    object[] blxls = new object[blxllists.Count()];
                                    int n = 0;
                                    foreach (BlXlInfo blxl in blxllists)
                                    {
                                        Hashtable bl = new Hashtable();
                                        bl.Add("cymc_bom_glass_id", blxl.Blxl_id);
                                        bl.Add("cymc_long", blxl.Changdu);
                                        bl.Add("cymc_wide", blxl.Kuandu);
                                        string[] value = blxl.WinBLZL.Split(' ');
                                        bl.Add("cymc_land", value[0]);
                                        bl.Add("cymc_color", value[1]);
                                        bl.Add("name", blxl.WinBLZL);
                                        bl.Add("glass_price", blxl.BLFee);
                                        bl.Add("product_qty", blxl.Shuliang);
                                        blxls[n] = bl;
                                        n++;
                                    }
                                    wd.Add("bom_glass_lines", blxls);
                                    windowdetails[j] = wd;
                                    
                                }
                                j++;
                            }
                            //添加工程门窗明细列表
                            if (windowdetaillists.Count() > 1)
                            {
                                w.Add("bom_lines", windowdetails);
                            }
                            windows[i] = w;
                            i++;
                        }
                        //添加工程门窗列表
                        project.Add("order_line", windows);

                        IProxy.IMethod method = XmlRpcProxyGen.Create<IProxy.IMethod>();
                        object res = method.execute(odoo_dbname, odoo_uid,
                            odoo_pwd, odoo_objname, "create_sale_order", Newtonsoft.Json.JsonConvert.SerializeObject(project));
                        ResData resData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResData>(res.ToString());
                        this.resultTextBox.Text = resData.successOrNot + "|" + resData.message + "|" + resData.data;
                        this.progressBar.Value = this.progressBar.Value + 1;
                    }
                }
                else
                {
                    MessageBox.Show("其他同步正在进行中，请稍候", "error");
                }
                flg = true;
                this.projectbutton.Enabled = true;
            }
            catch (Exception ex)
            {
                flg = true;
                this.projectbutton.Enabled = true;
                LogManager.WriteLog(LogFile.ERROR, ex.Message);
                MessageBox.Show("error", "error");
            }
        }
    }
}
